package com.example.music;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.example.music.DB.UserInfo;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.List;

public class LoginActivity extends AppCompatActivity {
    private SharedPreferences sharedPreferences;
    private Button login;
    private Button register;
    private EditText username;
    private EditText userpwd;
    public void init(){
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        username = findViewById(R.id.username);
        userpwd = findViewById(R.id.pwd);
    }

    public void initListener() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname = username.getText().toString();
                String upwd = userpwd.getText().toString();

                List<UserInfo> UserList = LitePal.select("userName,userPwd")
                        .where("userName = ?", uname)
                        .find(UserInfo.class);

                if(UserList!=null)
                    for (UserInfo useritem : UserList) {
                        if (useritem.getUserpwd().equals(upwd)) {
                            //修改全局状态
                            sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPreferences.edit();
                            editor.putBoolean("isLogin", true);
                            editor.putInt("curUserId",useritem.getId());
                            editor.putString("curUserName", useritem.getUsername());
                            editor.commit();

                            //跳转
                            jumpTo(HomeActivity.class);
                            showToast("登录成功！");
                            finish();
                            return;
                        }
                    }
                showToast("登录失败！用户名或密码错误！");
            }
        });

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String uname = username.getText().toString();
                String upwd = userpwd.getText().toString();
                List<UserInfo> UserList = LitePal.select("userName,userPwd").where("userName = ?", uname).find(UserInfo.class);
                if(!UserList.isEmpty()) {showToast("用户名已存在！"); return;}

                UserInfo user = new UserInfo(uname,upwd,false);
                user.save();
                showToast("注册成功，请您继续登录！");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        initListener();
    }

    public void jumpTo(Class context){
        Intent intent = new Intent(LoginActivity.this, context);
        startActivity(intent);
    }
    public void showToast(String msg){
        // 创建一个Toast消息
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}